package korkd.tower;

import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;


class Unit extends AnimatedSprite
{
	// Attributes
	private boolean controlled;
	private int count;
	private int level;
	private int health;
	private Rectangle mHealthBar;
	private boolean mDead;
	private UnitPool mUnitPool;

	// Constructor
	public Unit(float pX, float pY, TiledTextureRegion pTextureRegion, UnitPool pUnitPool) {
		super(pX, pY, pTextureRegion);
		this.setControlled(false);
		this.setLevel(1);
		this.health = 300;
		mUnitPool = pUnitPool;
		
		mDead = false;
		mHealthBar = new Rectangle(0, 0, this.getWidth(), 4);
		mHealthBar.setColor(1.0f, 0.0f, 0.0f, 1.0f);
		
		
		this.attachChild(mHealthBar);
	}

	public void hit(int damage)
	{
		int oldhealth = health;
		health -= damage;
		
		if (health <= 0 && oldhealth > 0)
		{
			this.kill();
			return;
		}
		
		float t = this.getWidth();
		t = t * (health/300.0f);
		mHealthBar.setWidth(t);
	}
	
	public void kill()
	{
		mUnitPool.recyclePoolItem(this);
		this.clearEntityModifiers();
	}
	
	public void attack()
	{
		mUnitPool.recyclePoolItem(this);
	}

	// BEGIN Setters and Getters
	// ****************************************************************
	public void setControlled(boolean controlled) {
		this.controlled = controlled;
	}

	public boolean isControlled() {
		return controlled;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getLevel() {
		return level;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}

	public Rectangle getHealthBar() {
		// TODO Auto-generated method stub
		return mHealthBar;
	}

	public int getHealth() {
		// TODO Auto-generated method stub
		return health;
	}

	public boolean isAlive() {
		// TODO Auto-generated method stub
		if (mDead)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	public void setDead(boolean b) {
		mDead = b;
	}

	public void setHealth(int i) {
		// TODO Auto-generated method stub
		health = i;
	}
}